import App from "./App.vue";
import Vue from "vue";
import router from "./router";
import store from "./store";


// console.log(process.env.NODE_ENV)

// if (process.env.NODE_ENV === 'development') {
//     console.log(1);
//     console.log('开发环境')
//    // window.baseUrl = "http://localhost:8080";
// }
// else if (process.env.NODE_ENV === 'production') {
//     console.log(2);
//     console.log('线上环境')
//    // window.baseUrl = window.location.host;
// }

// element样式
import { Message } from "element-ui";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import "@/assets/css/public.css";
Vue.use(ElementUI);

Vue.prototype.$message.error =  (msg)=> {
    return Message({
      type: 'error',
      message: msg,
        duration: 2000,
        center:true
    })
  }
Vue.prototype.$message.success =  (msg)=> {
    return Message({
        type: 'success',
        message: msg,
        duration: 1000,
        center:true
    })
}


//happy-scroll
import { HappyScroll } from "vue-happy-scroll";
//自定义组件名
Vue.component("happy-scroll", HappyScroll);
import "vue-happy-scroll/docs/happy-scroll.css";

// 自定义指令
import "./directive";

// 权限，数组对象
import permissions from "@/config/permissions.config.js";

// 加载工具函数，用于左侧menu
import { haveSame } from "@/utils/Tool.js";

// axios请求
import * as http from "@/request/http";
Vue.prototype.$axios = http;

//登录接口返回的permissions数组
let permissionsMy =
    localStorage.getItem("permissions") !== null ?
    JSON.parse(localStorage.getItem("permissions")) : [];

//全局用于添加组件中有初始接口的显示隐藏
Vue.prototype.$permissions = permissionsMy;

router.beforeEach((to, from, next) => {
    if (to.path === "/login") {
        return next();
    }
    //曙光
    if (to.path === "/LoginNoPwd") {
        return next();
    }
    let routes =
        localStorage.getItem("permissions") !== null ?
        JSON.parse(localStorage.getItem("permissions")) : [];
    var username = localStorage.getItem("username");
    //自定义的permissions.config.js对象数组
    var urls = routes[to.path];
    if (!urls) {
        if (username) {
            store.commit("login/setUser", localStorage.getItem("username"));
            next();
        } else {
            return next("/login");
            //曙光
            // return next("/LoginNoPwd");
        }
    } else {
        let result = haveSame(urls, routes); //返回true false
        if (username && result) {
            store.commit("login/setUser", localStorage.getItem("username"));
            next();
        } else if (username && !result) {
            ElementUI.Message.error("暂无权限！");
            next(from.path);
        } else {
            return next("/login");
        }
    }
});

new Vue({
    router,
    store,
    render: h => h(App)
}).$mount("#app");